package com.changgou.order.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * @ClassName OrderPayMessageListener
 * @Description
 * @Author 洛尘
 * @Date 20:44 2019/11/26
 * @Version 2.1
 **/
@Component
@RabbitListener(queues = {"${mq.pay.queue.order}"})
public class OrderPayMessageListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void readMessage(String msg ){
        Map<String , String> map = JSON.parseObject(msg, Map.class);
        String return_code = map.get("return_code");
        // 通信表示
        if ("SUCCESS".equals(return_code)){
            // 判断是否通信成功
            String result_code = map.get("result_code");
            if("SUCCESS".equals(result_code)){
                String out_trade_no = map.get("out_trade_no");
                // 更新订单
                if(out_trade_no != null) {
                    orderService.updateStatus(out_trade_no, map.get("transaction_id"));
                }
            }else{
                orderService.delateOrder(result_code);
            }
        }


    }

}
